/*     */ package com.zimbra.cs.service;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.AuthTokenException;
/*     */ import com.zimbra.cs.account.GuestAccount;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.fb.FreeBusy;
/*     */ import com.zimbra.cs.fb.FreeBusy.Method;
/*     */ import com.zimbra.cs.fb.FreeBusyQuery;
/*     */ import com.zimbra.cs.servlet.ZimbraServlet;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.IOException;
/*     */ import java.util.Collection;
/*     */ import java.util.Date;
/*     */ import java.util.Iterator;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.ServletOutputStream;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class PublicICalServlet
/*     */   extends ZimbraServlet
/*     */ {
/*     */   private static final long serialVersionUID = -7350146465570984660L;
/*  51 */   private static Log sLog = LogFactory.getLog(PublicICalServlet.class);
/*     */   private static final String QP_AUTH_TOKEN = "zauthtoken";
/*     */   
/*  54 */   public final void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { ZimbraLog.clearContext();
/*  55 */     String pathInfo = req.getPathInfo();
/*  56 */     if (pathInfo == null) {
/*  57 */       resp.sendError(400);
/*  58 */       return;
/*     */     }
/*  60 */     pathInfo = pathInfo.toLowerCase();
/*  61 */     boolean isReply = (pathInfo != null) && (pathInfo.endsWith("reply"));
/*  62 */     boolean isFreeBusy = (pathInfo != null) && (pathInfo.endsWith("freebusy.ifb"));
/*     */     
/*  64 */     if (isReply) {
/*  65 */       doReply(req, resp);
/*  66 */     } else if (isFreeBusy) {
/*  67 */       doGetFreeBusy(req, resp);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public final void doGetFreeBusy(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws IOException, ServletException
/*     */   {
/*  86 */     String acctName = req.getParameter("acct");
/*  87 */     String startStr = req.getParameter("s");
/*  88 */     String endStr = req.getParameter("e");
/*     */     
/*  90 */     resp.setContentType("text/calendar");
/*     */     
/*  92 */     if (checkBlankOrNull(resp, "acct", acctName)) {
/*  93 */       return;
/*     */     }
/*  95 */     long now = new Date().getTime();
/*     */     
/*  97 */     long rangeStart = now - 604800000L;
/*  98 */     long rangeEnd = now + 5356800000L;
/*     */     
/* 100 */     if (startStr != null) {
/* 101 */       rangeStart = Long.parseLong(startStr);
/*     */     }
/* 103 */     if (endStr != null) {
/* 104 */       rangeEnd = Long.parseLong(endStr);
/*     */     }
/* 106 */     if (rangeEnd < rangeStart) {
/* 107 */       resp.sendError(400, "End time must be after Start time");
/* 108 */       return;
/*     */     }
/*     */     
/* 111 */     long days = (rangeEnd - rangeStart) / 86400000L;
/* 112 */     long maxDays = LC.calendar_freebusy_max_days.longValueWithinRange(0L, 36600L);
/* 113 */     if (days > maxDays) {
/* 114 */       resp.sendError(400, "Requested range is too large (max " + maxDays + " days)");
/* 115 */       return;
/*     */     }
/*     */     
/* 118 */     String targetAccountId = null;
/*     */     try {
/* 120 */       Account targetAccount = Provisioning.getInstance().get(Key.AccountBy.name, acctName);
/* 121 */       if (targetAccount != null) {
/* 122 */         targetAccountId = targetAccount.getId();
/*     */       }
/*     */     } catch (ServiceException e) {}
/* 125 */     AuthToken at = getAuthTokenFromCookie(req, resp, true);
/* 126 */     if (at == null) {
/* 127 */       String authTokenParam = req.getParameter("zauthtoken");
/* 128 */       if (authTokenParam != null) {
/*     */         try {
/* 130 */           at = AuthProvider.getAuthToken(authTokenParam);
/*     */         } catch (AuthTokenException e) {
/* 132 */           sLog.warn("Auth error: " + e.getMessage(), e);
/*     */         }
/*     */       }
/*     */     }
/* 136 */     Account authAccount = null;
/* 137 */     ZimbraSoapContext zsc = null;
/* 138 */     if (at != null) {
/*     */       try {
/* 140 */         authAccount = Provisioning.getInstance().get(Key.AccountBy.id, at.getAccountId(), at);
/*     */       } catch (ServiceException e) {
/* 142 */         sLog.warn("Auth error: " + e.getMessage(), e);
/*     */       }
/*     */       try {
/* 145 */         zsc = new ZimbraSoapContext(at, targetAccountId, SoapProtocol.SoapJS, SoapProtocol.SoapJS);
/*     */       } catch (ServiceException e) {
/* 147 */         sLog.error("Error initializing request context", e);
/* 148 */         resp.sendError(500, "Error initializing request context");
/* 149 */         return;
/*     */       }
/*     */     } else {
/* 152 */       authAccount = GuestAccount.ANONYMOUS_ACCT;
/*     */     }
/*     */     
/* 155 */     FreeBusyQuery fbQuery = new FreeBusyQuery(req, zsc, authAccount, rangeStart, rangeEnd, null);
/* 156 */     fbQuery.addEmailAddress(acctName, -1);
/* 157 */     Collection<FreeBusy> result = fbQuery.getResults();
/* 158 */     FreeBusy fb = null;
/* 159 */     if (result.size() > 0) {
/* 160 */       fb = (FreeBusy)result.iterator().next();
/*     */     } else
/* 162 */       fb = FreeBusy.emptyFreeBusy(acctName, rangeStart, rangeEnd);
/* 163 */     String url = req.getRequestURL() + "?" + req.getQueryString();
/* 164 */     String fbMsg = fb.toVCalendar(FreeBusy.Method.PUBLISH, acctName, null, url);
/* 165 */     resp.getOutputStream().write(fbMsg.getBytes());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static final String QP_ACCOUNT = "acct";
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static final String QP_START_TIME = "s";
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static final String QP_END_TIME = "e";
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public final void doReply(HttpServletRequest req, HttpServletResponse resp) {}
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static boolean checkBlankOrNull(HttpServletResponse resp, String field, String value)
/*     */     throws IOException
/*     */   {
/* 325 */     if ((value == null) || (value.equals(""))) {
/* 326 */       resp.sendError(400, field + " required");
/* 327 */       return true;
/*     */     }
/* 329 */     return false;
/*     */   }
/*     */   
/*     */   public void init()
/*     */     throws ServletException
/*     */   {
/* 335 */     String name = getServletName();
/* 336 */     sLog.info("Servlet " + name + " starting up");
/* 337 */     super.init();
/*     */   }
/*     */   
/*     */   public void destroy()
/*     */   {
/* 342 */     String name = getServletName();
/* 343 */     sLog.info("Servlet " + name + " shutting down");
/* 344 */     super.destroy();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/PublicICalServlet.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */